package com.lyp.activitys;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.afollestad.materialdialogs.DialogAction;
import com.afollestad.materialdialogs.MaterialDialog;
import com.blankj.utilcode.util.LogUtils;
import com.lyp.adapters.IndoorAdapter;
import com.lyp.jsonbean.DetailsSingeBean;
import com.lyp.jsonbean.IndoorDetailsBean;
import com.lyp.jsonbean.JLSingeIndoorBean;
import com.lyp.jsonbean.LoginBean;
import com.tjl.yangxixis.R;
import com.tjl.yangxixis.api.YangxixiApi;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

//抢单(室内)详情
public class SingeIndoorActivity extends Activity implements View.OnClickListener{

    private JLSingeIndoorBean.DataBean indoorBean;
    private RecyclerView mRecyclerView;
    private LinearLayoutManager mLinearLayoutManager;
    private IndoorAdapter mAdapter;//室内
    private List<IndoorDetailsBean.DataBean> mList = new ArrayList<>();
    private TextView mTitle;
    private Button mOrder;
    private ImageView mBack;
    private LoginBean.DataBean bean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_singe_indoor);
        init();
    }

    private void init(){
        bean = (LoginBean.DataBean) getIntent().getExtras().get("bean");
        indoorBean = (JLSingeIndoorBean.DataBean) getIntent().getExtras().get("databean");
        mRecyclerView= (RecyclerView) findViewById(R.id.cardetails_recycler);
        mLinearLayoutManager = new LinearLayoutManager(this);
        mRecyclerView.setLayoutManager(mLinearLayoutManager);
        mAdapter = new IndoorAdapter(mList);
        mRecyclerView.setAdapter(mAdapter);
        try {
            Indoordetail(indoorBean.getO_id());
        } catch (IOException e) {
            e.printStackTrace();
        }

        mTitle = (TextView) findViewById(R.id.tv_cardetails_title);
        mTitle.setText("治理(居家服务)");
        //治理抢单按钮
        mOrder = (Button) findViewById(R.id.btn_order_singe);
        mBack = (ImageView) findViewById(R.id.iv_cardetails_back);
        mBack.setOnClickListener(this);
        mOrder.setOnClickListener(this);

    }

    //室内抢单
    public void Indoordetail(String o_id) throws IOException{
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(YangxixiApi.APP_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        YangxixiApi api = retrofit.create(YangxixiApi.class);
        Call<IndoorDetailsBean> call = api.getIndoorOrders(o_id);
        call.enqueue(new Callback<IndoorDetailsBean>() {
            @Override
            public void onResponse(Call<IndoorDetailsBean> call, final Response<IndoorDetailsBean> response) {
                if (response.body().getResult() == 1 ) {
                    SingeIndoorActivity.this.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            mOrder.setVisibility(View.VISIBLE);
                            mList.addAll(response.body().getData());
                            mAdapter.notifyDataSetChanged();
                        }
                    });
                }
            }

            @Override
            public void onFailure(Call<IndoorDetailsBean> call, Throwable t) {
                LogUtils.i("tag","失败"+t.getMessage());
            }
        });
    }


    //室内抢单按钮
    public void IndoorSinge(String o_id,String c_id) throws IOException {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(YangxixiApi.APP_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        YangxixiApi api = retrofit.create(YangxixiApi.class);
        Call<DetailsSingeBean> call = api.getDetailsSinge(o_id,c_id);
        call.enqueue(new Callback<DetailsSingeBean>() {
            @Override
            public void onResponse(Call<DetailsSingeBean> call, final Response<DetailsSingeBean> response) {
                if (response.body().getResult() == 1 ) {
                    new Handler().post(new Runnable() {
                        @Override
                        public void run() {
                            if (indoorBean.getCash_deposit() == 1) {
                            new MaterialDialog.Builder(SingeIndoorActivity.this)
                                    .title("抢单成功")
                                    .content("快去未分配任务进行分配!")
                                    .positiveText("确定")
                                    .canceledOnTouchOutside(false)
                                    .cancelable(false)
                                    .onPositive(new MaterialDialog.SingleButtonCallback(){
                                        @Override
                                        public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                                            setResult(RESULT_OK);
                                            finish();
                                        }
                                    })
                                    .show();
                            mAdapter.notifyDataSetChanged();
                            }
                            else if (indoorBean.getCash_deposit() == 0){
                                new MaterialDialog.Builder(SingeIndoorActivity.this)
                                        .title("无法抢单！")
                                        .content("请及时联系致蓝网络并缴纳保证金，未缴纳保证金只能抢单一次!")
                                        .positiveText("确定")
                                        .canceledOnTouchOutside(false)
                                        .cancelable(false)
                                        .onPositive(new MaterialDialog.SingleButtonCallback(){
                                            @Override
                                            public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                                                finish();
                                            }
                                        })
                                        .show();
                            }
                        }
                    });
                }
                else {
                    Toast.makeText(SingeIndoorActivity.this,response.body().getMessage(),Toast.LENGTH_SHORT).show();
                }
            }
            @Override
            public void onFailure(Call<DetailsSingeBean> call, Throwable t) {
                LogUtils.i("tag","失败"+t.getMessage());
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_order_singe:
                try {
                    IndoorSinge(indoorBean.getO_id(),bean.getC_id());
                } catch (IOException e) {
                    e.printStackTrace();
                }
                break;
            case R.id.iv_cardetails_back:
                finish();
                break;
        }
    }
}
